package com.example.shop_car_2208.fragment

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.GridLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.example.shop_car_2208.R
import com.example.shop_car_2208.adapter.GateAdapter
import com.example.shop_car_2208.adapter.GoodsAdapter
import com.example.shop_car_2208.databinding.FragmentIndexBinding
import com.example.shop_car_2208.entity.BannerResult
import com.example.shop_car_2208.entity.CateEntity

import com.example.shop_car_2208.mvvm.BaseFragment
import com.example.shop_car_2208.mvvm.BaseViewFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class HomeFragment : BaseFragment<FragmentIndexBinding,HomeViewModel>() {
    //存放轮播图地址集合
    val imagelist = mutableListOf<String>()
    val catrLiset = mutableListOf<CateEntity>()
    lateinit var goodsAdapter: GoodsAdapter
    lateinit var gateAdapter: GateAdapter
    var cateId = 14
    var page = 1
    val size = 10
    override fun initData() {
        viewModel.bannerSuccess.observe(this){
            if (it.code == 200){
                it.data.forEach{
                    imagelist.add(it.imagePath)
                }
                binding.banner.setImages(imagelist)
                    .setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if (context != null) {
                                if (imageView != null) {
                                    Glide.with(context).load(path).into(imageView)
                                }
                            }
                        }
                    }).setDelayTime(2000).setBannerStyle(BannerConfig.NUM_INDICATOR).start()
            }
        }
        //观察分类数据展示
        viewModel.cateSuccess.observe(this){
            if (it.code == 200){
                val data = it.data[0].data
                if (data != null) {
                    catrLiset.addAll(data)
                }
                gateAdapter.notifyDataSetChanged()
            }
        }
        //商品
        viewModel.goodsSuccess.observe(this){
            if (it.code == 200){
                if (page == 1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }

    }

    override fun initView() {
        //获取轮播图数据
        viewModel.banner()
        //初始化RecyclerView
        gateAdapter = GateAdapter(catrLiset)
        binding.rvCate.adapter = gateAdapter
        binding.rvCate.layoutManager = GridLayoutManager(context,5)
        //获取分类数据
        viewModel.getCate()
        //初始化商品
        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)
        viewModel.getGood(cateId,page,size)

        //刷新
        binding.srl.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.getGood(cateId,page,10)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                //加载更多---在后续数据上进行拼接
                page++
                viewModel.getGood(cateId,page,10)
                binding.srl.finishLoadMore()
            }

        })
        //点击分类，商品数据变化
        gateAdapter.setOnItemClickListener{adapter,view,position->
            gateAdapter.getSeleceId(gateAdapter.data[position].id)
            cateId = gateAdapter.data[position].id
            page = 1
            viewModel.getGood(cateId,page,10)

        }
        binding.query.setOnClickListener {
            ARouter.getInstance().build("/app/search").navigation()
        }
        //点击item调整到详情界面
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/detail").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }
    }

    override fun getViewModel(): Class<HomeViewModel> {
        return HomeViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.fragment_index
    }

}